/**
 * Copyright (c) [2025] [AGIROS] 
 * [travoddsgen] is licensed under Mulan PSL v2. 
 * You can use this software according to the terms and conditions of the Mulan PSL v2. 
 * You may obtain a copy of Mulan PSL v2 at: 
 *          http://license.coscl.org.cn/MulanPSL2 
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 
 * See the Mulan PSL v2 for more details.
 */ 

#include "SWIGPythonCMakeListsFileVisitor.h"
#include <fstream>

SWIGPythonCMakeListsFileVisitor::SWIGPythonCMakeListsFileVisitor(
    const std::string& outputDir, 
    std::string& targetName, 
    CppLangTypeSourceVisitor* typeSourceVisitor, 
    CppLangTypeSupportSourceVisitor* typeSupportSourceVisitor)
    : FileGenVisitor(outputDir, "CMakeLists.txt"), 
    typeSourceVisitor_(typeSourceVisitor), 
    typeSupportSourceVisitor_(typeSupportSourceVisitor), 
    targetName_(targetName) {
}

SWIGPythonCMakeListsFileVisitor::~SWIGPythonCMakeListsFileVisitor() {
}

void SWIGPythonCMakeListsFileVisitor::visit(SpecificationNode& node) {
    content_ += "cmake_minimum_required(VERSION 3.7)\n\n";
    content_ += "set(TARGET_NAME " + targetName_ + ")\n\n";
    content_ += "project(${TARGET_NAME})\n\n";
    content_ += "set(CMAKE_CXX_STANDARD 11)\n\n";
    
    // content_ += "set(travoddsz_DIR \"D:/travodds/travodds/install/cmake\")\n";
    // content_ += "set(travoddscppz_DIR \"D:/travodds/travodds_cpp/install/cmake\")\n";
    // content_ += "set(travoddspy_DIR \"D:/travodds/travodds_py/install/cmake\")\n";
    // content_ += "list(APPEND CMAKE_MODULE_PATH \"${travoddsz_DIR}\")\n";
    // content_ += "list(APPEND CMAKE_MODULE_PATH \"${travoddscppz_DIR}\")\n";
    // content_ += "list(APPEND CMAKE_MODULE_PATH \"${travoddspy_DIR}\")\n\n";

    content_ += "find_package(travoddscore REQUIRED)\n";
    content_ += "find_package(travoddscppz REQUIRED)\n";
    content_ += "find_package(travoddspy REQUIRED)\n";
    content_ += "find_package(SWIG REQUIRED)\n";
    content_ += "find_package(Python3 REQUIRED COMPONENTS Interpreter Development)\n\n";

    content_ += "set(SWIG_EXECUTABLE ${SWIG_EXECUTABLE})\n";
    content_ += "set(SWIG_INTERFACE_FILE ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_NAME}.i)\n";
    content_ += "set(SWIG_OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR})\n";
    content_ += "set(SWIG_OUTFILE ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_NAME}_wrap.cpp)\n\n";

    content_ += "set(SWIG_FLAGS -python -c++ -I${Python3_INCLUDE_DIRS} -I${travoddsz_INCLUDE_DIRS} -I${travoddscppz_INCLUDE_DIRS} -I${travoddspy_INCLUDE_DIRS} -outdir ${SWIG_OUTPUT_DIR} -o ${SWIG_OUTFILE})\n";
    content_ += "set(SWIG_COMMAND ${SWIG_EXECUTABLE} ${SWIG_FLAGS} ${SWIG_INTERFACE_FILE})\n\n";

    content_ += "add_custom_command(\n";
    content_ += "    OUTPUT ${SWIG_OUTFILE}\n";
    content_ += "    COMMAND ${SWIG_COMMAND}\n";
    content_ += "    DEPENDS ${SWIG_INTERFACE_FILE}\n";
    content_ += "    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}\n";
    content_ += "    COMMENT \"Generating SWIG wrapper code using ${SWIG_COMMAND}\"\n";
    content_ += ")\n\n";

    content_ += "add_custom_target(swig_${TARGET_NAME} ALL DEPENDS ${SWIG_OUTFILE})\n\n";

    content_ += "file(GLOB_RECURSE SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)\n";
    content_ += "add_library(${TARGET_NAME}_py SHARED ${SRC_FILES} ${SWIG_OUTFILE})\n";
   
    content_ += "target_compile_definitions(${TARGET_NAME}_py PRIVATE ${travoddsz_DEFINITIONS})\n"; 
    content_ += "target_include_directories(${TARGET_NAME}_py PRIVATE ${travoddsz_INCLUDE_DIRS})\n";
    content_ += "target_include_directories(${TARGET_NAME}_py PRIVATE ${travoddscppz_INCLUDE_DIRS})\n";
    content_ += "target_include_directories(${TARGET_NAME}_py PRIVATE ${Python3_INCLUDE_DIRS})\n";
    content_ += "set_target_properties(${TARGET_NAME}_py PROPERTIES OUTPUT_NAME \"_${TARGET_NAME}\")\n";
    content_ += "set_target_properties(${TARGET_NAME}_py PROPERTIES DEBUG_POSTFIX \"_d\")\n";
    content_ += "if (MSVC)\n";
    content_ += "set_target_properties(${TARGET_NAME}_py PROPERTIES SUFFIX \".pyd\")\n";
    content_ += "else()\n";
    content_ += "set_target_properties(${TARGET_NAME}_py PROPERTIES PREFIX \"\")\n";
    content_ += "endif()\n\n";
    content_ += "target_link_libraries(${TARGET_NAME}_py PRIVATE debug ${travoddscppzd_LIBRARIES})\n";
    content_ += "target_link_libraries(${TARGET_NAME}_py PRIVATE optimized ${travoddscppz_LIBRARIES})\n";
    content_ += "target_link_libraries(${TARGET_NAME}_py PRIVATE debug ${travoddszd_LIBRARIES})\n";
    content_ += "target_link_libraries(${TARGET_NAME}_py PRIVATE optimized ${travoddsz_LIBRARIES})\n";
    content_ += "target_link_libraries(${TARGET_NAME}_py PRIVATE ${Python3_LIBRARIES})\n";
    content_ += "add_dependencies(${TARGET_NAME}_py swig_${TARGET_NAME})\n\n";

    content_ += "if (NOT INSTALL_DIR)\n";
    content_ += "set(INSTALL_DIR \"${CMAKE_CURRENT_SOURCE_DIR}/install\")\n";
    content_ += "message(\"INSTALL_DIR is not set, using default value ${INSTALL_DIR}\")\n";
    content_ += "endif()\n\n";

    // 安装.py文件
    content_ += "install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_NAME}.py DESTINATION \"${INSTALL_DIR}/lib\")\n";
    content_ += "install(TARGETS ${TARGET_NAME}_py RUNTIME DESTINATION \"${INSTALL_DIR}/lib\" LIBRARY DESTINATION \"${INSTALL_DIR}/lib\" ARCHIVE DESTINATION \"${INSTALL_DIR}/lib\")\n";

    writeToFile();
}
